package com.dragon.mmochat.model.activity;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.model.enumType.MmochatMoneyType;
import com.dragon.mmochat.model.enumType.MmochatPayLotteryState;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.util.MmochatUtil;

//同一时间段，只能有一个此类活动
public class MmochatPayLotteryInfo {
	private int id;
	private MmochatPayLotteryState state = MmochatPayLotteryState.活动结束;
	private int moneyNeed; // 充值最低人民币
	private String startMsg;// 开始系统消息
	private String midMsg;// 开奖系统消息
	private String timeInfo; // 活动时间
	private MmochatMoneyType moneyType;// 礼物类型
	private int moneyAward; // 礼物数量
	private int lotteryNum; // 抽奖人数

	// 活动期间，所有充值的用户，可能有些未满足条件
	private Map<Integer, MmochatLotteryRole> payRoles = new ConcurrentHashMap<Integer, MmochatLotteryRole>();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MmochatPayLotteryState getState() {
		return state;
	}

	public void setState(MmochatPayLotteryState state) {
		this.state = state;
	}

	public int getMoneyNeed() {
		return moneyNeed;
	}

	public void setMoneyNeed(int moneyNeed) {
		this.moneyNeed = moneyNeed;
	}

	public String getStartMsg() {
		return startMsg;
	}

	public void setStartMsg(String startMsg) {
		this.startMsg = startMsg;
	}

	public String getMidMsg() {
		return midMsg;
	}

	public void setMidMsg(String midMsg) {
		this.midMsg = midMsg;
	}

	public MmochatMoneyType getMoneyType() {
		return moneyType;
	}

	public void setMoneyType(MmochatMoneyType moneyType) {
		this.moneyType = moneyType;
	}

	public int getMoneyAward() {
		return moneyAward;
	}

	public void setMoneyAward(int moneyAward) {
		this.moneyAward = moneyAward;
	}

	public String getTimeInfo() {
		return timeInfo;
	}

	public void setTimeInfo(String timeInfo) {
		this.timeInfo = timeInfo;
	}

	public int getLotteryNum() {
		return lotteryNum;
	}

	public void setLotteryNum(int lotteryNum) {
		this.lotteryNum = lotteryNum;
	}

	public Map<Integer, MmochatLotteryRole> getPayRoles() {
		return payRoles;
	}

	public void setPayRoles(Map<Integer, MmochatLotteryRole> payRoles) {
		this.payRoles = payRoles;
	}

	public void addRolePay(MmochatPlayer me, int addMoney) {
		MmochatLotteryRole role = payRoles.get(me.getRoleId());
		if (role == null) {
			role = new MmochatLotteryRole();
			role.setRoleId(me.getRoleId());
			role.setSkyId(me.getSkyId());
			role.setName(me.getName());
		}
		role.addMoney(addMoney);
		payRoles.put(role.getRoleId(), role);
	}

	// 计算符合抽奖条件的用户数
	public int calLotteryRoleNum() {
		int count = 0;
		for (MmochatLotteryRole role : payRoles.values()) {
			if (role.getMoney() >= getMoneyNeed()) {
				count++;
			}
		}
		return count;
	}

	// 获取中奖用户名单
	public List<MmochatLotteryRole> getHitRoles() {
		List<MmochatLotteryRole> list = new ArrayList<MmochatLotteryRole>();
		for (MmochatLotteryRole role : payRoles.values()) {
			if (role.isHit()) {
				list.add(role);
			}
		}
		return list;
	}

	// 进行抽奖
	public void doLottery() {
		if (payRoles != null && payRoles.size() > 0 && lotteryNum > 0) {
			List<MmochatLotteryRole> list = new ArrayList<MmochatLotteryRole>();
			// 所有满足抽奖条件的用户
			for (MmochatLotteryRole role : payRoles.values()) {
				if (role.getMoney() >= getMoneyNeed()) {
					list.add(role);
				}
			}

			// 抽出若干名
			int trueLotteryNum = Math.min(lotteryNum, list.size());
			for (int i = 0; i < trueLotteryNum; i++) {
				if (list.size() > 0) {
					int index = MmochatUtil.getRandomValue(list.size());
					MmochatLotteryRole role = list.remove(index);
					role.setHit(true);

					// 保存数据库
					try {
						MmochatDao.addNewActivityRecord2(role.getSkyId(), role
								.getRoleId(), moneyAward, id, role.getName());
					} catch (Exception e) {
						e.printStackTrace();
						return;
					}

					// 发送客服消息
					String userInfo = "恭喜您在本轮限时充值抽大奖活动中被系统抽中，获得"
							+ MmochatUtil.wrapColor(moneyAward
									+ moneyType.toString(), Color.red);
					if (moneyType == MmochatMoneyType.K币) {
						userInfo += "(K币奖励将在7个工作日内统一发放)!";
					} else if (moneyType == MmochatMoneyType.人民币) {
						userInfo += "(现金奖励由客服发放，请注意客服消息通知)!";
					} else {
						userInfo += "(奖励已发放给您，请查收)!";
					}
					userInfo = MmochatUtil.wrapColor(userInfo, Color.yellow);
					MmochatGmService.sendKufuPrivateMsg(role.getRoleId(),
							userInfo, false);

					// 如果是金钱或元宝，则马上下发奖励(不能大于10000000，防止数据异常)
					int moneyCount = moneyAward;
					if (moneyCount > 0 && moneyCount < 10000000) {
						switch (moneyType) {
						case 元宝: {
							MmochatPlayer me = MmochatGmService
									.getRoleInfoById(role.getRoleId());
							me.addBig_money(moneyCount);
							try {
								MmochatDao.addRoleBigMoney(me.getRoleId(),
										moneyCount);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						}
						case 金钱: {
							MmochatPlayer me = MmochatGmService
									.getRoleInfoById(role.getRoleId());
							try {
								me.addAndUpdateSmall_money(moneyCount);
							} catch (Exception e1) {
								e1.printStackTrace();
							}
							try {
								MmochatDao.addRoleSmallMoney(me.getRoleId(),
										moneyCount);
							} catch (Exception e) {
								e.printStackTrace();
							}
							break;
						}
						default:
							break;
						}
					}
				}
			}
		}
	}

}
